package com.ruijie.acl.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.model.SelectItem;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import com.ruijie.acl.AccessGroup;
import com.ruijie.acl.AclInterface;
import com.ruijie.acl.AclInterfaceView;
import com.ruijie.acl.AclPlanInterface;
import com.ruijie.acl.service.AceService;
import com.ruijie.acl.service.AclInterfaceService;
import com.ruijie.acl.task.config.TaskConfigAclDevice;
import com.ruijie.acl.task.model.AccessGroupInfo;
import com.ruijie.acl.task.model.AclInterfaceInfo;
import com.ruijie.acl.utils.AclConfigUtil;
import com.ruijie.acl.utils.AclUtil;
import com.ruijie.acl.utils.InterfaceSortUtil;
import com.ruijie.batch.plan.PlanParameters;
import com.ruijie.emp.device.Device;
import com.ruijie.foundation.bo.EntityObject;
import com.ruijie.foundation.exception.BusinessException;
import com.ruijie.foundation.service.Repository;


/**
 * <p>
 * Title: ACL部署计划管理
 * </p>
 * <p>
 * Description: Function Description
 * </p>
 * <p>
 * Copyright: Copyright (c) 2009
 * </p>
 * <p>
 * Company: Ruijie Co., Ltd.
 * </p>
 * <p>
 * Create Time: 2009-09-21
 * </p>
 * 
 * @author ASAI
 *         <p>
 *         Update Time:
 *         </p>
 *         <p>
 *         Updater:
 *         </p>
 *         <p>
 *         Update Comments:
 *         </p>
 */
@Name("aclInterfaceSelectAction")
@Scope(ScopeType.CONVERSATION)
public class AclInterfaceSelectAction extends AclBaseAction {

	
	@In("#{aclInterfaceService}")
	private AclInterfaceService aclInterfaceService;
	
	@In("#{taskConfigAclDevice}")
	private TaskConfigAclDevice taskConfigAclDevice;
	
	@In("#{aceService}")
	private AceService aceService;

	private Device device;
	
	private AccessGroup accessGroup;

	private List<AclInterfaceView> aclInterfaceView;

	private String selectedInterfaceByAccessGroup;
	
	private String initInterfaceByAccessGroup;//用于存储初始的接口值

	private Map<Long, List<AclInterface>> interfaceMap = new HashMap<Long, List<AclInterface>>();
	
	private List<AclInterface> aclInterfaceList = new ArrayList<AclInterface>();
	
	private String accessGroupName;
	
	@In("#{accessGroupAction}")
	private AccessGroupAction accessGroupAction;

	public String getAccessGroupName() {
		accessGroupName = AclUtil.escapeSpecialString(accessGroup.getName());
		return accessGroupName;
	}

	public void setAccessGroupName(String accessGroupName) {
		this.accessGroupName = accessGroupName;
	}

	/**
	 * 取得设备的所有接口
	 * 
	 * @param device
	 *            设备
	 * @return 接口列表
	 */
	public List<AclInterfaceView> getAclInterfaceView(Device device) {
		if (aclInterfaceView == null || this.device == null) {
			this.device = device;
			aclInterfaceView = aclInterfaceService
					.getAclInterfaceViewByDevice(device);
			aclInterfaceView = InterfaceSortUtil
			.sortInterfaceItemList(aclInterfaceView);
		}
		return aclInterfaceView;
	}

	/**
	 * 取得设备的所有接口
	 * 
	 * @param device
	 *            设备
	 * @return 接口列表
	 */
	public List<AclInterfaceView> getAclInterfaceView() {
		if (this.device != null) {
			aclInterfaceView = aclInterfaceService
					.getAclInterfaceViewByDevice(this.device);
			aclInterfaceView = InterfaceSortUtil
					.sortInterfaceItemList(aclInterfaceView);
		}
		return aclInterfaceView;
	}

	/**
	 * 取得设备的所有接口
	 * 
	 * @param device
	 *            设备
	 * @return 接口列表
	 */
	public List<SelectItem> getAclInterface() {
		if (this.device != null) {
			this.aclInterfaceView = null;
			return InterfaceSortUtil
					.sortInterfaceItemListSelect(getAclInterfaceView(this.device));
		}
		return new ArrayList<SelectItem>();
	}
	
	/**
	 * 部署ACL组到接口
	 * @param id
	 * @param device
	 */
	public void reConfig(Device device,AccessGroup group){
		try {
			List<AccessGroup> accessGroupList = new ArrayList<AccessGroup>();
			accessGroupList.add(accessGroup);

			//部署
			aclInterfaceService.reConfigSelectInterface(device, accessGroupList, selectedInterfaceByAccessGroup);
			//获得AclInterface
			List<AclInterface> aclInterfaceList = convertString2AclInterface(this.selectedInterfaceByAccessGroup,group);
			aclInterfaceService.removeByDevice(device);//删除acl接口
			for(AclInterface entity:aclInterfaceList){//重新保存接口
				aclInterfaceService.save(entity);
			}
			accessGroupAction.setModel(group);
			redirectTo("/acl/accessGroupDetail.seam");
		} catch (BusinessException e) {
			selectedInterfaceByAccessGroup=initInterfaceByAccessGroup;
			throw e;
		}	
	}
	
	public String convertAclPlanInterface2String(Device device) {
		return convertAclPlanInterface2String(device.getId());
	}
	
	public String convertAclPlanInterface2String(Long id) {
		if (this.interfaceMap.containsKey(id)) {
			return convertAclPlanInterface2String(this.interfaceMap.get(id));
		}
		return "";
	}
	
	public String convertAclPlanInterface2String(
			List<AclInterface> aclInterfaceList) {
		String resultString = "";
		if (aclInterfaceList != null && aclInterfaceList.size() > 0) {
			for (AclInterface aclInterface : aclInterfaceList) {
				resultString += aclInterface.getDeviceInterface().getId()
						+ ",";
				if (aclInterface.getInAccessGroup() != null && aclInterface.getOutAccessGroup() != null) {
					resultString += "InOut," + aclInterface.getInAccessGroup().getId() + ";";
				}else if (aclInterface.getInAccessGroup() != null) {
					resultString += "In," + aclInterface.getInAccessGroup().getId() + ";";
				}else if (aclInterface.getOutAccessGroup() != null) {
					resultString += "Out," + aclInterface.getOutAccessGroup().getId() + ";";
				}
			}
		}
		return resultString;
	}
	
	/**
	 * 把字符串转换成接口类型
	 * @param planInterfaceString 
	 * @return
	 */
	public List<AclInterface> convertString2AclInterface(String planInterfaceString,AccessGroup accessGroup) {
		List<AclInterface> aclInterfaceList = null;
		if (!AclUtil.empty(planInterfaceString)) {
			aclInterfaceList = new ArrayList<AclInterface>();
			String[] arrAclInterfaceList = planInterfaceString.split(";");
			for (String interfaceStr : arrAclInterfaceList) {
				String[] arrAclInterfaces = interfaceStr.split(",");
				if (arrAclInterfaces.length == 3) {
					AclInterface interfaceInfo = new AclInterface();
					interfaceInfo.setDevice(device);
					interfaceInfo.setDeviceInterface(aclInterfaceService.getDeviceInterface(Long.valueOf(arrAclInterfaces[0])));
					if("InOut".equals(arrAclInterfaces[1])){
						interfaceInfo.setInAccessGroup(accessGroup);
						interfaceInfo.setOutAccessGroup(accessGroup);
						interfaceInfo.setInAccessGroupName(accessGroup.getName());
						interfaceInfo.setOutAccessGroupName(accessGroup.getName());
					}else if("In".equals(arrAclInterfaces[1])){
						interfaceInfo.setInAccessGroup(accessGroup);
						interfaceInfo.setInAccessGroupName(accessGroup.getName());
					}else{
						interfaceInfo.setOutAccessGroup(accessGroup);
						interfaceInfo.setOutAccessGroupName(accessGroup.getName());
					}
					aclInterfaceList.add(interfaceInfo);
				}
			}
		}
		return aclInterfaceList;
	}

	public Device getDevice() {
		return device;
	}

	public void setDevice(Device device) {
		this.device = device;
	}

	public String getSelectedInterfaceByAccessGroup() {
		return selectedInterfaceByAccessGroup;
	}

	public void setSelectedInterfaceByAccessGroup(
			String selectedInterfaceByAccessGroup) {
		this.selectedInterfaceByAccessGroup = selectedInterfaceByAccessGroup;
	}

	public Map<Long, List<AclInterface>> getInterfaceMap() {
		return interfaceMap;
	}

	public void setInterfaceMap(Map<Long, List<AclInterface>> interfaceMap) {
		this.interfaceMap = interfaceMap;
	}
	
	
	@SuppressWarnings("unchecked")
	@Override
	protected Repository getRepository() {
		return this.aclInterfaceService;
	}
	
	public String getSelectPage(Device device,AccessGroup accessGroup){
		this.device = device;
		this.accessGroup = accessGroup;
		selectedInterfaceByAccessGroup ="";
		initInterfaceByAccessGroup = "";
		
		List<AclInterface> aclInterfaceList = aclInterfaceService.getAclInterfaces(device, accessGroup);
		interfaceMap.put(device.getId(), aclInterfaceList);
		if (this.interfaceMap.containsKey(device.getId())) {
			this.aclInterfaceList = this.interfaceMap.get(device.getId());
			selectedInterfaceByAccessGroup = convertAclPlanInterface2String(this.aclInterfaceList);
			initInterfaceByAccessGroup = selectedInterfaceByAccessGroup;
		}else{
			selectedInterfaceByAccessGroup = "";
			initInterfaceByAccessGroup = "";
		}
		
		return "/acl/selectInterface.seam";
	}
	
	public List<AclInterface> getAclInterface(Device device){
		if (device != null){
			return this.getInterfaceMap().get(device.getId());
		}else{
			return new ArrayList<AclInterface>();
		}
	}

	@Override
	protected String getAddPage() {
		return "/acl/timeRangeAdd.seam";
	}

	@Override
	public EntityObject getCriteria() {
		return null;
	}

	@Override
	protected String getDetailPage() {
		return "/acl/timeRangeDetail.seam";
	}

	@Override
	protected String getListPage() {
		return "/acl/timeRangeIndex.seam";
	}

	@Override
	public EntityObject getModel() {
		return null;
	}

	@Override
	protected String getUpdatePage() {
		return "/acl/timeRangeEdit.seam";
	}

	@Override
	public void resetModel() {
		
	}
	
	@Override
	public void setCriteria(EntityObject criteria) {
		
	}

	@Override
	public void setModel(EntityObject model) {
		
	}

	public AccessGroup getAccessGroup() {
		return accessGroup;
	}

	public void setAccessGroup(AccessGroup accessGroup) {
		this.accessGroup = accessGroup;
	}

	public String getInitInterfaceByAccessGroup() {
		return initInterfaceByAccessGroup;
	}

	public void setInitInterfaceByAccessGroup(String initInterfaceByAccessGroup) {
		this.initInterfaceByAccessGroup = initInterfaceByAccessGroup;
	}
}
